import { Post } from '@midwayjs/decorator';
import { Body } from '@midwayjs/decorator';
import { InjectEntityModel } from '@midwayjs/orm';
import { ALL, Get, Inject, Provide, Query } from '@midwayjs/decorator';
import { CoolController, BaseController, BaseService } from 'midwayjs-cool-core';
import { Context } from 'vm';
import { CourseTypeEntity } from '../../../courseType/entity/courseType';
import { courseSourceEntity } from '../../entity/courseSource';
import { CourseSourceService } from '../../service/courseSource';
import { Repository } from 'typeorm';

/**
 * 描述
 */
@Provide()
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: courseSourceEntity,
  service:CourseSourceService,
  pageQueryOp:{ 
    // 让title字段支持模糊查询
    keyWordLikeFields: ['section_theme',"course_gbc","program_design","mechanical"],
    // 让type字段支持筛选
    // 指定返回字段
    // select: ['a.*', 'b.course_id'],
    // // 关联表用户表
    // leftJoin: [{
    //   entity: CourseTypeEntity,
    //   alias: 'b',
    //   condition: 'a.course_type = b.id'
    // }],
    fieldEq: ['course_type','level','isUse','phase_name'],
    where: async (ctx: Context) => { 
      return [ 
        ['a.isDelete = :isDelete', { isDelete: false }] 
      ] 
    },
    // 添加排序
    addOrderBy: {
      updateTime: "desc",
      // phase_sort: 'desc',
    }
  },
  listQueryOp:{ 
    // 让type字段支持筛选
    fieldEq: ['course_type',"course_gbc","program_design","mechanical"],
    // 指定返回字段
    select: ['a.*', 'b.course_id'],
    // 关联表用户表
    leftJoin: [{
      entity: CourseTypeEntity,
      alias: 'b',
      condition: 'a.course_type = b.id'
    }],
    where: async (ctx: Context) => { 
      return [ 
        ['a.isDelete = :isDelete', { isDelete: false }] 
      ] 
    },
    // 添加排序
    addOrderBy: {
      updateTime: "desc",
      // phase_sort: 'desc',
    }
  }
})
export class courseSourceAdminController extends BaseController {

  @Inject()
  service:BaseService

  @InjectEntityModel(courseSourceEntity)
  courseSourceEntity: Repository<courseSourceEntity>;

  @Get('/infoRaw')
  async classCourseStudents(@Query(ALL) queryObject: any) {
    const {id} = queryObject
    console.log(id,'--______--');
    

    const info = await this.service.nativeQuery(
      `
      select  a.id,a.createTime,a.updateTime,a.note,a.section_theme,a.program_design,a.mechanical,a.scientific,a.teacher_experience,a.child_five_theme,a.course_from,a.course_gbc,a.course_time,a.isUse,a.learning_goals,a.sort,a.phase_sort,c.phase_name,b.course_name,b.course_id as course_type,a.level,a.file,a.isDelete
      from course_source a
      left join course_type b on a.course_type = b.id
      left join course_system c on a.phase_name = c.id
      where a.id = ${id} and a.isDelete = false
      `  
    )
    
    return {code:1000,data:info}
  }
  @Post('/updateSort')
  async updateSort(@Body(ALL) queryObject: any) {
    // const {id} = queryObject
    // console.log(id,'--______--');
    console.log(queryObject,"11111111111111");
    if (queryObject.data){
      queryObject.data.forEach(async element => {
        await this.courseSourceEntity.update(element.id,{phase_sort:element.sort})
      });
    }

    // const info = await this.service.nativeQuery(
    //   `
    //   select  a.id,a.createTime,a.updateTime,a.note,a.section_theme,a.program_design,a.mechanical,a.teacher_experience,a.child_five_theme,a.course_from,a.course_shape,a.course_time,a.isUse,a.learning_goals,a.sort,a.phase_sort,c.phase_name,b.course_name,b.course_id as course_type,a.level,a.file,a.isDelete
    //   from course_source a
    //   left join course_type b on a.course_type = b.id
    //   left join course_system c on a.phase_name = c.id
    //   where a.id = ${id} and a.isDelete = false
    //   `  
    // )
    
    return {code:1000,data:"success"}
  }
}
